{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 直接将像素的信息作为数值使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[255, 255, 170, 34, 102, 238, 255, 255, 255, 255, 34, 0, 85, 0, 170, 255, 255, 204, 0, 221, 255, 68, 119, 255, 255, 187, 51, 255, 255, 119, 119, 255, 255, 170, 119, 255, 255, 102, 119, 255, 255, 187, 68, 255, 238, 51, 136, 255, 255, 221, 17, 170, 85, 51, 255, 255, 255, 255, 153, 34, 85, 255, 255, 255]\n"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "img = Image.open('mlzukan-img.png').convert('L')\n",
    "width, height = img.size\n",
    "img_pixels = []\n",
    "for y in range(height):\n",
    "    for x in range(width):\n",
    "        # 通过getpixel获取指定位置的像素值\n",
    "        img_pixels.append(img.getpixel((x,y)))\n",
    "\n",
    "print(img_pixels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 将转换后的向量数据作为输入来应用机器学习模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "              precision    recall  f1-score   support\n",
      "\n",
      "           0       0.97      1.00      0.98        88\n",
      "           1       0.87      0.84      0.85        91\n",
      "           2       0.91      0.90      0.90        86\n",
      "           3       0.88      0.79      0.83        91\n",
      "           4       0.95      0.90      0.93        92\n",
      "           5       0.87      0.87      0.87        91\n",
      "           6       0.91      0.93      0.92        91\n",
      "           7       0.94      0.98      0.96        89\n",
      "           8       0.76      0.80      0.78        88\n",
      "           9       0.77      0.82      0.79        92\n",
      "\n",
      "   micro avg       0.88      0.88      0.88       899\n",
      "   macro avg       0.88      0.88      0.88       899\n",
      "weighted avg       0.88      0.88      0.88       899\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn import datasets\n",
    "from sklearn import metrics\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "digits = datasets.load_digits()\n",
    "\n",
    "n_samples = len(digits.images)\n",
    "data = digits.images.reshape((n_samples, -1))\n",
    "\n",
    "model = RandomForestClassifier(n_estimators=10)\n",
    "\n",
    "model.fit(data[:n_samples // 2], digits.target[:n_samples // 2])\n",
    "\n",
    "expected = digits.target[n_samples // 2:]\n",
    "predicted = model.predict(data[n_samples // 2:])\n",
    "\n",
    "print(metrics.classification_report(expected, predicted))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
